package cn.gudian.cms.Comment

class CommentController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    def mode = params.mode
    def modeID = params.modeID
    def commentInstanceList = null;
    def commentInstanceTotal = null;
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort = params.sort ? params.sort : "id"
    params.order = params.order ? params.order : "desc"
    if (!mode || !modeID) {
      commentInstanceList = Comment.list(params)
      commentInstanceTotal = Comment.count()
    } else {
      commentInstanceList = Comment.findAllByModeAndModeID(mode, modeID, params)
      commentInstanceTotal = Comment.countByModeAndModeID(mode, modeID)
    }

    [mode: mode, modeID: modeID, commentInstanceList: commentInstanceList, commentInstanceTotal: commentInstanceTotal]
  }

  def create = {
    def commentInstance = new Comment()
    commentInstance.properties = params
    return [commentInstance: commentInstance]
  }

  def save = {
    def commentInstance = new Comment(params)
    commentInstance.subTime = new Date();
    commentInstance.reTime = new Date();
    commentInstance.ip = request.getRemoteAddr();
    commentInstance.audit = false;
    def url= request.getHeader("REFERER")+"#sendComment"
    if (!commentInstance.hasErrors() && commentInstance.save()) {
      flash.message = "评论成功，审核后将会显示。"
      flash.args = [commentInstance.id]
      flash.defaultMessage = "Comment ${commentInstance.id} created"
      redirect(url:url)
    }
    else {
      render(view: "create", model: [commentInstance: commentInstance])
    }
  }

  def show = {
    def commentInstance = Comment.get(params.id)
    if (!commentInstance) {
      flash.message = "comment.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Comment not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [commentInstance: commentInstance]
    }
  }

  def edit = {
    def commentInstance = Comment.get(params.id)
    if (!commentInstance) {
      flash.message = "comment.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Comment not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [commentInstance: commentInstance]
    }
  }

  def update = {
    def commentInstance = Comment.get(params.id)
    if (commentInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (commentInstance.version > version) {

          commentInstance.errors.rejectValue("version", "comment.optimistic.locking.failure", "Another user has updated this Comment while you were editing")
          render(view: "edit", model: [commentInstance: commentInstance])
          return
        }
      }
      commentInstance.properties = params
      if (!commentInstance.hasErrors() && commentInstance.save()) {
        flash.message = "comment.updated"
        flash.args = [params.id]
        flash.defaultMessage = "Comment ${params.id} updated"
        redirect(action: "list", id: commentInstance.id, params: [mode: params?.mode, modeID: params?.modeID])
      }
      else {
        render(view: "edit", model: [commentInstance: commentInstance])
      }
    }
    else {
      flash.message = "comment.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Comment not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def commentInstance = Comment.get(params.id)
    if (commentInstance) {
      try {
        commentInstance.delete()
        flash.message = "comment.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Comment ${params.id} deleted"
        redirect(action: "list", params: [mode: commentInstance?.mode, modeID: commentInstance?.modeID])
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "comment.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Comment ${params.id} could not be deleted"
        redirect(action: "show", id: params.id, params: [mode: commentInstance?.mode, modeID: commentInstance?.modeID])
      }
    }
    else {
      flash.message = "comment.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Comment not found with id ${params.id}"
      redirect(action: "list", params: [mode: commentInstance?.mode, modeID: commentInstance?.modeID])
    }
  }

  def deleteAll = {
    def mode = params.mode
    def modeID = params.modeID
    def qstr = params.qstr
    if (qstr) {
      try {
        Comment.executeUpdate("delete Comment where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list", params: [mode: mode, modeID: modeID])
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list", params: [mode: mode, modeID: modeID])
      }
    }
  }

  def audit = {
    def mode = params.mode
    def modeID = params.modeID
    def qstr = params.qstr
    if (qstr) {
      try {
        Comment.executeUpdate("update Comment set audit=true where id in(" + qstr + ")")
        flash.message = "审核成功:" + qstr
        redirect(action: "list", params: [mode: mode, modeID: modeID])
      } catch (Exception e) {
        flash.message = "审核失败:" + qstr
        redirect(action: "list", params: [mode: mode, modeID: modeID])
      }
    }
  }
}
